const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    sexlist:['男','女'],
    name:'',
    sex:'',
    idcardNumber:'',
    nationality:'',
    birthday:"",
    phone:'',
    edu:'',
    school:'',
    detail:'',
    age:'',
  },
  onShow: function (options) {
    this.resumedetail()
  },
  resumedetail(){
    Controller.resumedetail().then(res=>{
      if(res.data.baseData){
        this.setData({
          detail:res.data.baseData,
          name:res.data.baseData.name,
          sex:res.data.baseData.sex==1?'男':'女',
          idcardNumber:res.data.baseData.idcardNumber,
          nationality:res.data.baseData.nationality,
          birthday:res.data.baseData.birthday,
          phone:res.data.baseData.phone,
          edu:res.data.baseData.edu,
          school:res.data.baseData.school,
          age:res.data.baseData.age
        })
      }
      this.setData({
        isloading:false
      })
    })
  },
  changeinput(e){
    let data = {};
    data[e.currentTarget.dataset.name]=e.detail.value
    this.setData(data)
  },
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value,
      sex:this.data.sexlist[e.detail.value]
    })
  },
  bindPickerChange2: function(e) {
    this.setData({
      birthday: e.detail.value,
      age:this.getAge(e.detail.value)
    })
  },
  golink(e){
    wx.navigateTo({
      url:e.currentTarget.dataset.url
    });
    app.globalData.resumeitem=e.currentTarget.dataset.item
    console.log(app.globalData.resumeitem)
  },
  getAge(birthday) {
    var returnAge = '';
    var mouthAge = '';
    var strBirthdayArr = birthday.split("-");
    var birthYear = strBirthdayArr[0];
    var birthMonth = strBirthdayArr[1];
    var birthDay = strBirthdayArr[2];
    var d = new Date();
    var nowYear = d.getFullYear();
    var nowMonth = d.getMonth() + 1;
    var nowDay = d.getDate();
    if (nowYear == birthYear) {
      // returnAge = 0; //同年 则为0岁
      var monthDiff = nowMonth - birthMonth; //月之差 
      if (monthDiff < 0) {
      } else {
        mouthAge = monthDiff ;
      }
    } else {
      var ageDiff = nowYear - birthYear; //年之差
      if (ageDiff > 0) {
        if (nowMonth == birthMonth) {
          var dayDiff = nowDay - birthDay; //日之差 
          if (dayDiff < 0) {
            returnAge = ageDiff - 1 ;
          } else {
            returnAge = ageDiff ;
          }
        } else {
          var monthDiff = nowMonth - birthMonth; //月之差 
          if (monthDiff < 0) {
            returnAge = ageDiff - 1 ;
          } else {
            mouthAge = monthDiff ;
            returnAge = ageDiff ;
          }
        }
      } else {
        returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
      }
    }
    return returnAge; //返回周岁年龄+月份
  },
  save(){
    if(!this.data.name){
      wx.showToast({
        title:"请输入姓名",
        icon:"none"
      })
      return
    }
    if(!this.data.sex){
      wx.showToast({
        title:"请输入性别",
        icon:"none"
      })
      return
    }
    if(!this.data.nationality){
      wx.showToast({
        title:"请输入民族",
        icon:"none"
      })
      return
    }
    if(!this.data.birthday){
      wx.showToast({
        title:"请输入出生年月日",
        icon:"none"
      })
      return
    }
    if(!this.data.age){
      wx.showToast({
        title:"请输入年龄",
        icon:"none"
      })
      return
    }
    if(!this.data.phone){
      wx.showToast({
        title:"请输入手机号码",
        icon:"none"
      })
      return
    }
    if (this.data.phone && !/^1([0123356789][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(this.data.phone)) {
        wx.showToast({
          title: "手机号码格式错误",
          icon: 'none'
        });
        return
    }
   
    var data={
      name:this.data.name || '',
      sex:this.data.sex=='男'?1:2,
      idcardNumber:this.data.idcardNumber || '',
      nationality:this.data.nationality || '',
      birthday:this.data.birthday || '',
      phone:this.data.phone || '',
      edu:this.data.edu || '',
      school:this.data.school || '',
      age:this.data.age || ''
    }
    Controller.resumeupdate(data).then(res=>{
      wx.showToast({
        title:"保存成功",
        icon: 'success',
        duration: 2000
      })
      setTimeout(res => {
        wx.navigateBack({ //返回
          delta: 1
        })
      },2000)
    })
  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})